<template>
  <div>
    <el-dialog v-model="dialogVisible" width="80%" align-center :close-on-click-modal="false" :show-close="false"
      :before-close="handleClose" destroy-on-close draggable>
      <template #header="{ close }">
        <div class="dialog-header flex justify-between items-center">
          <div>讲座{{ ruleForm.id ? '编辑' : '新增' }}</div>
          <el-icon class="el-icon--left cursor-pointer" @click="close">
            <CircleCloseFilled color="#fff" />
          </el-icon>
        </div>
      </template>
      <el-form :model="ruleForm" :rules="rules" label-width="130px" ref="ruleFormRef">
        <!-- 基本信息 -->
        <div class="bg-white mt-[10px]">
          <BaseHead>基本信息</BaseHead>
          <div class="p-[10px]">

            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="讲座名称" prop="name">
                  <el-input placeholder="请输入讲座名称" v-model="ruleForm.name" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="讲座类型" prop="lectureTypeId">
                  <el-radio-group v-model="ruleForm.lectureTypeId">
                    <el-radio v-for="item in jzlxList" :key="item.code" :value="item.code" :label="item.name"
                      size="large"></el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="讲座日期" prop="lectureDate">
                  <el-date-picker v-model="ruleForm.lectureDate" type="date" format="YYYY/MM/DD"
                    value-format="YYYY-MM-DD" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="讲座级别">
                  <el-radio-group v-model="ruleForm.lectureLevelId">
                    <el-radio v-for="item in jzjbList" :key="item.code" :value="item.code" :label="item.name"
                      size="large"></el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="主讲人" prop="lecturer">
                  <el-input placeholder="请输入主讲人" v-model="ruleForm.lecturer" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="所属单位" prop="unitId">
                  <el-select v-model="ruleForm.unitId" placeholder="请选择所属单位" clearable>
                    <el-option v-for="item in zcgsdwList" :key="item.codeColumn" :label="item.valueColumn"
                      :value="item.codeColumn" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="观点综述">
                  <el-input v-model="ruleForm.intro" type="textarea" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="会议相关文档">
                  <FileUpload :uploadUrl="uploadUrl" :limit="1" tip="最多可上传1个附件,每个附件大小不超过20M" v-model="fileList"
                    @change="fileChange">
                  </FileUpload>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </div>
        <!-- 详细信息 -->
        <div class="bg-white mt-[10px]">
          <BaseHead>详细信息</BaseHead>
          <div class="p-[10px]">

            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="主讲人单位">
                  <el-input placeholder="请输入主讲人单位" v-model="ruleForm.lecturerUnit" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="主讲人职务">
                  <el-input placeholder="请输入主讲人职务" v-model="ruleForm.lecturerDuty" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="主讲人职称">
                  <el-input placeholder="请输入主讲人职称" v-model="ruleForm.lecturerTitle" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="主持人">
                  <el-input placeholder="请输入主持人" v-model="ruleForm.compere" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="嘉宾">
                  <el-input placeholder="请输入嘉宾" v-model="ruleForm.commend" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="讲座对象">
                  <el-input placeholder="请输入讲座对象" v-model="ruleForm.lectureListener" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="参加讲座人数">
                  <el-input placeholder="请输入参加讲座人数" type="number" v-model="ruleForm.listenerNumber" @input="handleInput"
                    clearable>
                    <template #append>人</template></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="讲座地点">
                  <el-input placeholder="请输入讲座地点" v-model="ruleForm.address" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="主讲人学术简历">
                  <el-input v-model="ruleForm.lecturerInfo" type="textarea" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </div>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <div class="operate-btns">
            <el-button type="primary" @click="submitForm(ruleFormRef, '-1')" v-if="ruleForm.checkstatus != '2'"
              :loading="btnLoading">暂存</el-button>
            <el-button type="primary" @click="submitForm(ruleFormRef)" v-if="checkEditButtonT()"
              :loading="btnLoading">{{
      Cookies.get("role-key") ==
        'biz_kyms' ? '提交' : ruleForm.checkstatus == '2' ? '保存' :
        '保存并通过' }}</el-button>
            <el-button type="primary" @click="initData()">关闭</el-button>
          </div>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import BaseHead from "@/views/project/components/BaseHead.vue";
import FileUpload from "@/views/achievement/components/FileUpload.vue"
import { getlectureAdd, getlectureUpdate } from "@/api/achievement/xsjl"
import { ElMessage, ElMessageBox } from "element-plus";
import Cookies from 'js-cookie'
const dialogVisible = defineModel();
const props = defineProps({
  jzlxList: Array,
  jzjbList: Array,
  zcgsdwList: Array,
})

const ruleForm = reactive({
  name: '',
  lectureTypeId: '',
  lectureDate: '',
  lectureLevelId: '',
  lecturer: '',
  unitId: '',
  intro: '',
  lecturerUnit: '',
  lecturerTitle: '',
  lecturerDuty: '',
  compere: '',
  commend: '',
  lectureListener: '',
  listenerNumber: null,
  address: '',
  lecturerInfo: '',
  fileIds: '',
})
const rules = reactive({
  name: [
    { required: true, message: '请输入讲座名称', trigger: 'blur' },
  ],
  lecturer: [
    { required: true, message: '请输入主讲人', trigger: 'blur' },
  ],
  unitId: [
    {
      required: true,
      message: '请选择所属单位',
      trigger: 'change',
    },
  ],
  lectureTypeId: [
    {
      required: true,
      message: '请选择讲座类型',
      trigger: 'change',
    },
  ],
  lectureDate: [
    {
      required: true,
      message: '请选择讲座日期',
      trigger: 'change',
    },
  ],
})
const btnLoading = ref(false)
const ruleFormRef = ref()
const submitForm = async (formEl, i) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      btnLoading.value = true;
      let params = {
        ...ruleForm,
      }
      params.submitType = i ? i : Cookies.get("role-key") == 'biz_kyms' ? '1' : '2'
      // if (i == '-1') {
      //   if (!ruleForm.id || Cookies.get("role-key") == 'biz_kyms') {
      //   } else {
      //     params.submitType = params.checkstatus
      //   }
      // }
      console.log(params, 'params')
      if (params.id) {
        getlectureUpdate(params).then((res) => {
          if (res.code == 200) {
            ElMessage({
              message: res.msg,
              type: 'success',
            })
            initData()
            emit('callBack', "")
          } else {
            ElMessage({
              message: res.msg,
              type: 'error',
            })
          }
          btnLoading.value = false;
        })
      } else {
        getlectureAdd(params).then((res) => {
          if (res.code == 200) {
            ElMessage({
              message: res.msg,
              type: 'success',
            })
            initData()
            emit('callBack', "")
          } else {
            ElMessage({
              message: res.msg,
              type: 'error',
            })
          }
          btnLoading.value = false;
        })
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}
const handleInput = (value) => {
  if (!/^[1-9]\d*$/.test(value)) { // 正则表达式验证正整数
    ElMessage.error('请输入正整数');
    ruleForm.listenerNumber = null // 移除最后一个非法字符，或者设置为空字符串也可以，看具体需求
  }
}
const emit = defineEmits(['callBack']);
const handleClose = (done) => {
  Object.assign(ruleForm, {
    name: '',
    lectureTypeId: '',
    lectureDate: '',
    lectureLevelId: '',
    lecturer: '',
    unitId: '',
    intro: '',
    lecturerUnit: '',
    lecturerTitle: '',
    lecturerDuty: '',
    compere: '',
    commend: '',
    lectureListener: '',
    listenerNumber: null,
    address: '',
    lecturerInfo: '',
    fileIds: '',
  })
  fileList.value = []
  delete ruleForm.id;
  delete ruleForm.checkstatus;
  emit('callBack', "")
  done()
}
const initData = () => {
  Object.assign(ruleForm, {
    name: '',
    lectureTypeId: '',
    lectureDate: '',
    lectureLevelId: '',
    lecturer: '',
    unitId: '',
    intro: '',
    lecturerUnit: '',
    lecturerTitle: '',
    lecturerDuty: '',
    compere: '',
    commend: '',
    lectureListener: '',
    listenerNumber: null,
    address: '',
    lecturerInfo: '',
    fileIds: '',
  })
  fileList.value = []
  delete ruleForm.id;
  delete ruleForm.checkstatus;
  dialogVisible.value = false
}
const getRuleForm = (i) => {
  for (const key in i) {
    ruleForm[key] = i[key];
  }
  fileList.value = i.fileList.map((item) => {
    return {
      name: item.fileName,
      id: item.id,
    };
  });
  console.log(ruleForm, 'ruleForm')
}
const uploadUrl = `/commonFile/uploadFile?modelName=lectureApply`;
const fileList = ref([]);
//上传的文件改变
const fileChange = () => {
  const ids = fileList.value.map((item) => {
    const id = item.response ? item.response.data : item.id;
    return id;
  });
  ruleForm.fileIds = ids.join(",");
};

const checkEditButtonT = () => {
  if (Cookies.get("role-key") == 'biz_kyms') {
    return true
  } else if (Cookies.get("role-key") == 'biz_kycgly') {
    if (ruleForm.checkstatus == '1') {
      return ruleForm.isAuditor ? true : false
    } else {
      return true
    }
  } else {
    return false
  }
}


defineExpose({
  getRuleForm
})
</script>

<style scoped lang="scss">
@import url("@/views/achievement/index.scss");
</style>
